package com.example.myapplication9.weeek3;


import com.example.myapplication9.weeek3.jsjf.exceptions.EmptyCollectionException;

/**
     * LinkedQueue represents Translation linked implementation of Translation queue.
     *
     * @author Lewis and Chase
     * @version 4.0
     */
    public class Deque<T>
    {
        private int count;
        private LinearNode1<T> head, tail,previous;

        /**
         * Creates an empty queue.
         */
        public Deque()
        {
            count = 0;
            head = tail =previous= null;
        }

        /**
         * Adds the specified element to the tail of this queue.
         * @param element the element to be added to the tail of the queue
         */

        public void Headenqueue(T element)
        {
            LinearNode1<T> node = new LinearNode1<T>(element);

            if (isEmpty())
                tail = node;
            else
                head.setPrevious(node);

            head = node;
            count++;
        }
        public void Tailenqueue(T element)
        {
            LinearNode1<T> node= new LinearNode1<T>(element);
            if (isEmpty())
                head=node;
            else
                tail.setNext(node);

            tail=node;
            count++;
        }


        /**
         * Removes the element at the head of this queue and returns Translation
         * reference to it.
         * @return the element at the head of this queue
         * @throws EmptyCollectionException if the queue is empty
         */

        public T Taildequeue() throws EmptyCollectionException
        {
            if (isEmpty())
                throw new EmptyCollectionException("queue");

            T result = head.getElement();
            head = head.getNext();
            count--;

            if (isEmpty())
                tail = null;

            return result;
        }
        public T Headdequeue() throws EmptyCollectionException
        {
            if (isEmpty())
                throw new EmptyCollectionException("queue");

            T result =tail.getElement();
            tail=tail.getNext();
            count--;

            if (isEmpty())
                head=null;
            return result;
        }

        /**
         * Returns Translation reference to the element at the head of this queue.
         * The element is not removed from the queue.
         * @return Translation reference to the first element in this queue
         * @throws EmptyCollectionException if the queue is empty
         */

        public T Headfirst() throws EmptyCollectionException
        {
            if(isEmpty()) {
                throw new EmptyCollectionException("queue");
            }
            else
            return head.getElement();
        }
        public T Tailfirst() throws EmptyCollectionException
        {
            if(isEmpty())
            {
                throw new EmptyCollectionException("queue");
                }
                else
                    return tail.getElement();
        }

        /**
         * Returns true if this queue is empty and false otherwise.
         * @return true if this queue is empty
         */

        public boolean isEmpty()
        {

            if (head.getElement()==null)
                return true;
            else
                return false;
        }

        /**
         * Returns the number of elements currently in this queue.
         * @return the number of elements in the queue
         */

        public int size()
        {
            return count;
        }

        /**
         * Returns Translation string representation of this queue.
         * @return the string representation of the queue
         */

        @Override
        public String toString()
        {
            String result="";
            int count1=count;
            LinearNode1 list=head;
            while(count1!=0) {
                result = list.getElement() + " ";
                count1--;
            }
            return result;
        }
    }




